package com.bsg.upm.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.BackupStrategyService;
import com.bsg.upm.service.DatabaseService;
import com.bsg.upm.service.ServDirectUserService;
import com.bsg.upm.service.ServService;
import com.bsg.upm.service.ServUserService;
import com.bsg.upm.service.SubServParamterConfigService;
import com.bsg.upm.service.SubServService;
import com.bsg.upm.util.DateUtils;

/**
 * 服务管理控制层
 * 
 * @author HCK
 *
 */
@Controller
@RequestMapping("/v1.0/servs")
public class ServController {

	@Resource
	private ServService servService;

	@Resource
	private SubServService subServService;

	@Resource
	private SubServParamterConfigService subServParamterConfigService;

	@Resource
	private DatabaseService databaseService;

	@Resource
	private BackupStrategyService backupStrategyService;

	@Resource
	private ServUserService servUserService;

	@Resource
	private ServDirectUserService servDirectUserService;

	/**
	 * 【服务查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listServ(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			if (paramMap.containsKey("site")) {
				searchParamMap.put("siteId", paramMap.get("site"));
			}
			if (paramMap.containsKey("serv_type")) {
				searchParamMap.put("definitionServCode", paramMap.get("serv_type"));
			}
			respJson = servService.list(searchParamMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【服务详情】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getServ(@PathVariable("serv_id") String servId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servService.get(servId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【服务变更】
	 * 
	 * @param servId
	 *            服务编码
	 * @param type
	 *            变更类型(scale_up：扩容；image_update：版本变更)
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateServ(@PathVariable("serv_id") String servId, @RequestParam("type") String type,
			@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("servId", servId);
			respJson = servService.update(paramMap, type);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【服务启动】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/start", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson startServ(@PathVariable("serv_id") String servId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servService.start(servId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【服务停止】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/stop", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson stopServ(@PathVariable("serv_id") String servId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servService.stop(servId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【服务备份】
	 * 
	 * @param servId
	 *            服务编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/backup", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson backupServ(@PathVariable("serv_id") String servId,
			@RequestBody(required = false) Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			if (paramMap == null) {
				paramMap = new HashMap<>();
			}
			paramMap.put("servId", servId);
			paramMap.put("remark", "手动备份");
			respJson = servService.backup(paramMap, false);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【服务重建】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/rebuild", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson rebuildServ(@PathVariable("serv_id") String servId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servService.rebuild(servId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【服务删除】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeServ(@PathVariable("serv_id") String servId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servService.remove(servId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 服务下载
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse对象
	 */
	@RequestMapping("/download")
	public void download(@RequestParam Map<String, Object> paramMap, HttpServletRequest request,
			HttpServletResponse response) {
		OutputStream os = null;
		Workbook wb = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			if (paramMap.containsKey("site")) {
				searchParamMap.put("siteId", paramMap.get("site"));
			}
			if (paramMap.containsKey("serv_type")) {
				searchParamMap.put("definitionServCode", paramMap.get("serv_type"));
			}
			String filePath = request.getSession().getServletContext().getRealPath("WEB-INF/file/") + File.separator
					+ "serv_download_template.xls";
			String fileName = "托管区DBAAS报表" + DateUtils.dateToString(new Date());
			fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
			response.setContentType("application/vnd.ms-excel");
			response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
			os = response.getOutputStream();
			wb = servService.download(filePath, searchParamMap);
			wb.write(os);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.flush();
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (wb != null) {
				try {
					wb.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 【子服务查询】
	 * 
	 * @param servId
	 *            服务编码
	 * @param subServType
	 *            子服务类型
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/subservs/{subserv_type}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listSubServ(@PathVariable("serv_id") String servId,
			@PathVariable("subserv_type") String subServType, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = subServService.list(servId, subServType);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【子服务配置文件查询】
	 * 
	 * @param servId
	 *            服务编码
	 * @param subServType
	 *            子服务类型
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/subservs/{subserv_type}/configs", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listConfigs(@PathVariable("serv_id") String servId,
			@PathVariable("subserv_type") String subServType, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = subServParamterConfigService.list(servId, subServType);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【子服务配置文件编辑】
	 * 
	 * @param servId
	 *            服务编码
	 * @param subServType
	 *            子服务类型
	 * @param paramMaps
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/subservs/{subserv_type}/configs", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateConfigs(@PathVariable("serv_id") String servId,
			@PathVariable("subserv_type") String subServType, @RequestBody List<Map<String, Object>> paramMaps,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = subServParamterConfigService.update(servId, subServType, paramMaps);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【数据库查询】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/databases", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listDatabase(@PathVariable("serv_id") String servId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = databaseService.list(servId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【数据库新增】
	 * 
	 * @param servId
	 *            服务编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/databases", method = RequestMethod.POST)
	@ResponseBody
	public RespJson saveDatabase(@PathVariable("serv_id") String servId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("servId", servId);
			respJson = databaseService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【数据库编辑】
	 * 
	 * @param servId
	 *            服务编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/databases/{name}", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateDatabase(@PathVariable("serv_id") String servId, @PathVariable("name") String name,
			@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("servId", servId);
			paramMap.put("name", name);
			respJson = databaseService.update(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【数据库删除】
	 * 
	 * @param servId
	 *            服务编码
	 * @param databaseName
	 *            数据库名
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/databases/{name}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeDatabase(@PathVariable("serv_id") String servId, @PathVariable("name") String databaseName,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = databaseService.remove(servId, databaseName);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【查询备份策略】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/backup_strategys", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listBackupStartegy(@PathVariable("serv_id") String servId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = backupStrategyService.list(servId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【查询备份策略详细】
	 * 
	 * @param servId
	 *            服务编码
	 * @param backupStrategyId
	 *            备份策略编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/backup_strategys/{backup_strategy_id}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getBackupStartegy(@PathVariable("serv_id") String servId,
			@PathVariable("backup_strategy_id") String backupStrategyId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = backupStrategyService.get(backupStrategyId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【新增备份策略】
	 * 
	 * @param servId
	 *            服务编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/backup_strategys", method = RequestMethod.POST)
	@ResponseBody
	public RespJson saveBackupStartegy(@PathVariable("serv_id") String servId,
			@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("servId", servId);
			respJson = backupStrategyService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【编辑备份策略】
	 * 
	 * @param servId
	 *            服务编码
	 * @param backupStrategyId
	 *            备份策略编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/backup_strategys/{backup_strategy_id}", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateBackupStartegy(@PathVariable("serv_id") String servId,
			@PathVariable("backup_strategy_id") String backupStrategyId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("servId", servId);
			paramMap.put("backupStrategyId", backupStrategyId);
			respJson = backupStrategyService.update(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【启用备份策略】
	 * 
	 * @param servId
	 *            服务编码
	 * @param backupStrategyId
	 *            备份策略编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/backup_strategys/{backup_strategy_id}/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableBackupStartegy(@PathVariable("serv_id") String servId,
			@PathVariable("backup_strategy_id") String backupStrategyId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = backupStrategyService.enable(backupStrategyId, true);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【停用备份策略】
	 * 
	 * @param servId
	 *            服务编码
	 * @param backupStrategyId
	 *            备份策略编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/backup_strategys/{backup_strategy_id}/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableBackupStartegy(@PathVariable("serv_id") String servId,
			@PathVariable("backup_strategy_id") String backupStrategyId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = backupStrategyService.enable(backupStrategyId, false);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【删除备份策略】
	 * 
	 * @param servId
	 *            服务编码
	 * @param backupStrategyId
	 *            备份策略编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/backup_strategys/{backup_strategy_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeBackupStartegy(@PathVariable("serv_id") String servId,
			@PathVariable("backup_strategy_id") String backupStrategyId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = backupStrategyService.remove(backupStrategyId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【查询用户】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/users", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listUser(@PathVariable("serv_id") String servId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servUserService.list(servId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【用户详细】
	 * 
	 * @param servId
	 *            服务编码
	 * @param username
	 *            用户名
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/users/{username}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getUser(@PathVariable("serv_id") String servId, @PathVariable("username") String username,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servUserService.get(servId, username);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【新增用户】
	 * 
	 * @param servId
	 *            服务编码
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/users", method = RequestMethod.POST)
	@ResponseBody
	public RespJson saveUser(@PathVariable("serv_id") String servId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("servId", servId);
			respJson = servUserService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【编辑用户】
	 * 
	 * @param servId
	 *            服务编码
	 * @param username
	 *            用户名
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/users/{username}", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateUser(@PathVariable("serv_id") String servId, @PathVariable("username") String username,
			@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("servId", servId);
			paramMap.put("username", username);
			respJson = servUserService.update(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【删除用户】
	 * 
	 * @param servId
	 *            服务编码
	 * @param username
	 *            用户名
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/users/{username}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeUser(@PathVariable("serv_id") String servId, @PathVariable("username") String username,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servUserService.remove(servId, username);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【查询直连用户】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/direct_users", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listDirectUser(@PathVariable("serv_id") String servId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servDirectUserService.list(servId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【保存直连用户】
	 * 
	 * @param servId
	 *            服务编码
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/direct_users", method = RequestMethod.POST)
	@ResponseBody
	public RespJson saveDirectUser(@PathVariable("serv_id") String servId, @RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			paramMap.put("servId", servId);
			respJson = servDirectUserService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【删除直连用户】
	 * 
	 * @param servId
	 *            服务编码
	 * @param username
	 *            用户名
	 * @param ip
	 *            IP地址
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/{serv_id}/direct_users/{username}", method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removeDirectUser(@PathVariable("serv_id") String servId, @PathVariable("username") String username,
			@RequestParam("ip") String ip, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = servDirectUserService.remove(servId, username, ip);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

}
